package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.PageDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.enums.HttpEnum;
import com.itheima.reggie.service.DishService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 菜品管理(Dish)表控制层
 *
 * @author shenjiaqi
 * @since 2024-02-02 00:02:51
 */
@RestController
@RequestMapping("dish")
@Slf4j
public class DishController {
    /**
     * 服务对象
     */
    @Resource
    private DishService dishService;

    /**
     * 菜品分页
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public R page(PageDto dto) {
        return dishService.pageList(dto);
    }

    /**
     * 菜品新增
     *
     * @param
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dto) {
        return dishService.add(dto);
    }

    /**
     * 菜品回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return dishService.getDetailsById(id);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @PutMapping
    public R updateByDetail(@RequestBody DishDto dto) {
        return dishService.updateByDetail(dto);
    }

    @PostMapping("/status/{status}")
    public R updateWithStatus(@NonNull HttpServletRequest request, @PathVariable String status, @RequestParam("ids") List<String> ids) {
        String requestUrl = request.getRequestURL().toString();
        log.info("{}", requestUrl);
        String status1 = requestUrl.substring(requestUrl.lastIndexOf("/") + 1);
        return dishService.updateStatus(status, ids);

    }

    // @PostMapping("/status/0")
    // public R upStatus(@NonNull HttpServletRequest request, @RequestParam("ids") List<String> ids) {
    //     String requestUrl = request.getRequestURL().toString();
    //     log.info("{}", requestUrl);
    //     String status = requestUrl.substring(requestUrl.lastIndexOf("/") + 1);
    //     return dishService.updateStatus(status, ids);
    // }
    //
    //
    // @PostMapping("/status/1")
    // public R downStatus(@NonNull HttpServletRequest request, @RequestParam("ids") List<String> ids) {
    //     String requestUrl = request.getRequestURL().toString();
    //     log.info("{}", requestUrl);
    //     String status = requestUrl.substring(requestUrl.lastIndexOf("/") + 1);
    //     return dishService.updateStatus(status, ids);
    // }

    /**
     * 根据分类Id获取对应详情
     * 这里为菜品详情
     *
     * @param categoryId
     * @param name
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(required = false) Long categoryId, @RequestParam(required = false) String name) {
        if (categoryId == null && StringUtils.isBlank(name)) {
            return R.error(HttpEnum.PARAMETER_NULL.getMsg());
        }
        List<Dish> dishList = dishService.list(Wrappers.<Dish>lambdaQuery()
                .eq(null != categoryId, Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, 1)
                .like(StringUtils.isNotBlank(name), Dish::getName, name)
                .orderByDesc(Dish::getUpdateTime)
                .orderByAsc(Dish::getSort));
        return R.success(dishList);
    }
}

